/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkHttpHooker/MkHttpClientPool.h"
#include "MkEvent/MkEvent.h"
#include "MkEvent/MkEventPool.h"
#include "MkEvent/MkTimer.h"
#include <MkUtil/MkLog.h>
#include <MkUtil/MkUtil.h>
#include <signal.h>

MkVector<CMkEvent *> VecEvent;
BOOL g_bRun = TRUE;
void SigHandle(int sig)
{
    if (sig == SIGINT) {
        g_bRun = FALSE;
    }
};

int main(int argc, char ** argv)
{
#ifdef _WIN32
    WSADATA WSAData;
    WSAStartup(0x101, &WSAData);
#else
    //signal(SIGPIPE, SIG_IGN);  //ignore sigpipe
    //signal(SIGPIPE, SIG_IGN);
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGPIPE);
    sigprocmask(SIG_BLOCK, &set, NULL);
#endif
    MkLogInitLevel("sampleHttpClientPool", TO_FILE|TO_SCREEN);
    MkLogChangeLevel(LOG_DEBUG);
    signal(SIGINT, SigHandle);
    CMkEventPool Pool;
    Pool.Start(1);
    CMkTimer Timer;
    Timer.StartTimerThread();
    CMkHttpClientPool  *pClientPool = new  CMkHttpClientPool(&Pool, &Timer);
    g_bRun = TRUE;
    Json::Value JsonReq;
    
    MkString Url = "http://192.168.100.25:7480/pic/ImageLimpid/positives/1688054736807501826791.jpg";
    MkString Ur2 = "http://zyspzx.scdl.cn:30101/spzx-oss-picture-dev/16951193560569955040@16951193561329040040/2023/9/1695245403761468.jpg";
    JsonReq["picUrl"] = Url;
    Json::Value AnalyseArray;
    AnalyseArray[0]["cmd"] = "ImageLimpid";
    AnalyseArray[1]["cmd"] = "WhiteBlack";
    AnalyseArray[2]["cmd"] = "ImageNoisy";
    AnalyseArray[3]["cmd"] = "ImageDetectColor";
    JsonReq["analysis"] = AnalyseArray;
    MkString  Url1 = "http://192.168.100.120:58096/api/v1/image-analysis";
    Url1 = "http://192.168.0.24:58096/api/v1/image-analysis";

    int Index = 0;
    MkMap<MkString, MkString> MapHeader;
    MapHeader["Authorization"] = "Basic YWRtaW46YWRtaW4=";
    while (g_bRun) {
        Index++;
        if (pClientPool) {
            //pClientPool->SendRequest(MkHttpMethodPost, Url1, MkMapString(), JsonReq.toStyledString(), MapHeader, [](const MkHttpStatusCode& StatusCode, const MkString& Res) {
            //    MkDebugLog("recv response code:%d \n", StatusCode);
            //},10000);
            MkString ReqUrl = /*CMkUtil::GetRandomNumber()*/Index % 2 ? Url : Ur2;
            pClientPool->SendRequest(MkHttpMethodGet, ReqUrl, MkMapString(), "", MkMapString(), [](const MkHttpStatusCode& StatusCode, const MkString& Res) {
                MkDebugLog("recv response code:%d \n", StatusCode);
            }, 1000);
            CMkThread::ThreadSleep(20);
        }
    }
    Timer.StopTimerThread();
#ifdef _WIN32
    WSACleanup();
#endif
    return 0;
}
